HackerRank 3D Surface Area
https://www.hackerrank.com/challenges/3d-surface-area/problem
提出
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A, H, W):
# Write your code here
# print(A)
# 1, 3, 4], 2, 2, 3, [1, 2, 4
# 9, (1+2+4), 9, (1+3+4), (1+2+1), (4+3+4), 5, 7
first = H * W
second = 0
for i in A:
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0)
W = int(first_multiple_input1)
A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A):
# Write your code here
# print(A)
# 1, 3, 4], 2, 2, 3, [1, 2, 4
# top and buttom
area = 2 * H * W
# checks the limit
def check(i, j):
return Ax+iy+j if 0 <= x+i < H and 0 <= y+j < W else 0
# remaining surfaces
xi = 0, 0, 1, -1
yi = 1, -1, 0, 0
for x in range(H):
for y in range(W):
# (0, 1), (0, -1), (1, 0), (1, 1)
for i, j in zip(xi, yi):
# gives area from adjacent cells
area += max(0, Axy - check(i, j))
return area
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0)
W = int(first_multiple_input1)
A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=GYaLhyfGRhM
提出
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A, H, W):
# Write your code here
# 9 + 9 + 11 + 10 + 10 + 11
topNum = H * W
downNum = H * W
front = []
frontAns = []
# A = 1, 3, 4], 2, 2, 3, [1, 2, 4
for a in A:
front.append([a0])
frontAns.append(a0)
for i in range(len(A)):
for j, a in enumerate(Ai):
if j == 0:
continue
else:
if a > fronti-1:
frontAns.append(a - fronti-1)
fronti.append(a)
frontNum = sum(frontAns)
B = []
back = []
backAns = []
for a in A:
B.append(list(reversed(a)))
# B = 4, 3, 1], 3, 2, 2, [4, 2, 1
for b in B:
back.append([b0])
backAns.append(b0)
for i in range(len(A)):
for j, b in enumerate(Bi):
if j == 0:
continue
else:
if b > backi-1:
backAns.append(b - backi-1)
backi.append(b)
backNum = sum(backAns)
L = [[] for _ in range(len(A))]
left = []
leftAns = []
for i in range(len(A)):
for a in A:
Li.append(ai)
# L = 1, 2, 1], 3, 2, 2, [4, 3, 4
for l in L:
left.append([l0])
leftAns.append(l0)
for i in range(len(A)):
for j, l in enumerate(Li):
if j == 0:
continue
else:
if l > lefti-1:
leftAns.append(l - lefti-1)
lefti.append(l)
leftNum = sum(leftAns)
R = []
right = []
rightAns = []
for l in L:
R.append(list(reversed(l)))
# R = 1, 2, 1], 2, 2, 3, [4, 3, 4
for r in R:
right.append([r0])
rightAns.append(r0)
for i in range(len(A)):
for j, r in enumerate(Ri):
if j == 0:
continue
else:
if r > righti-1:
rightAns.append(r - righti-1)
righti.append(r)
print(rightAns)
rightNum = sum(rightAns)
return topNum + downNum + frontNum + backNum + leftNum + rightNum
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0)
W = int(first_multiple_input1)
A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()